import dt from '@dt/dt'
import Opt from '@/components/dt/info/Opt.js'
import houseOpt from '@/biz/option/house.js'
import businessCommonMgr from '@/biz/businessReport/businessCommon'

let _this = null
let companys = []
class SearchOpt extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  getCompany() {
    let opt = {
      title: '组织筛选',
      key: 'orgIds',
      placeholder: '全部',
      tree: {
        options: companys,
        saveAll: true
      },
      change: (v) => {
        console.log(v)
        let companyIds = [],
          officeIds = []
        v && v.editValue.forEach(item => {
          if (item.level == 2) {
            companyIds.push(item.id)
          } else if (item.level == 3) {
            officeIds.push(item.id)
          }
        });
        v.info.companyIds = companyIds
        v.info.officeIds = officeIds
      }
    }
    let params = {
      isTwoLevel: true,
    }
    businessCommonMgr
      .getOrgThreeTree(params)
      .then((res) => {
        companys = res
        opt.tree.options = companys
      })
    return opt
  }

  createAll() {
    let callTimeChange = {
      title: '回拨时间',
      placeholder: '请选择时间范围',
      key: 'callbackTime',
      date: {
        type: 'daterange',
        disabled: (date) => {
          console.log(_this.info.clueGenStartTime, '&&&&')
          let startDate = _this.info.clueGen_Time[0] ? _this.info.clueGen_Time[0] : ''
          return date.valueOf() < new Date(startDate).getTime()
        },
      },
      disabled: true,
      data: {
        to: (val) => {
          return {
            callbackStartTime: val[0],
            callbackEndTime: val[1]
          }
        }
      },
    }
    this.all = [
      this.getCompany(),
      houseOpt.create({
        title: '所属案场',
        to: (info) => {
          return {
            buildingId: info.housesId
          }
        }
      }),
      {
        title: '线索生成时间',
        placeholder: '请选择时间范围',
        key: 'clueGen_Time',
        date: {
          type: 'daterange'
        },
        change: (val) => {
          console.log(val)
          if (val.value[0] || _this.info.batch) {
            callTimeChange.disabled = false
            _this.info.callbackTime = []
          } else {
            callTimeChange.disabled = true
            _this.info.callbackTime = []
          }
        },
        // data: {
        //   to: (val) => {
        //     return {
        //       clueGenStartTime: val[0],
        //       clueGenEndTime: val[1]
        //     }
        //   }
        // },
      },
      {
        title: '导入批次',
        placeholder: '请输入批次名',
        key: 'batchName',
        input: {},
        change: (val) => {
          console.log(val)
          if (val.value || _this.info.createTime[0]) {
            callTimeChange.disabled = false
          } else {
            callTimeChange.disabled = true
            _this.info.callbackTime = []
          }
        }
      },
      callTimeChange,
      {
        title: '导入渠道',
        placeholder: '请输入导入渠道',
        key: 'channelSource',
        select: {
          options: [{ key: '同策好房', value: '同策好房' }, { key: '客户复用', value: '客户复用' }]
        }
      },
    ]
  }


}

export default SearchOpt